package com.deyuanyun.pic.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.deyuanyun.pic.business.controller.dto.PipeLineNewTaskVO;
import com.deyuanyun.pic.business.domain.MergeTaskBasis;
import com.deyuanyun.pic.business.service.MergeTaskService;

/**
 * Created by linwei on 12/7/2016.
 */
@Service
public class MergeTaskServiceImpl implements MergeTaskService {

    
    public List<PipeLineNewTaskVO> mergeTask(List<MergeTaskBasis> list){
        
        List<PipeLineNewTaskVO> pipeLineExinspTaskList = new ArrayList<PipeLineNewTaskVO>();
        //得到管道的每段绝对距离段的最新任务
        for(MergeTaskBasis task : list )
        {
            String id = task.getId();
            Long beginMileage = task.getFromPointMileage();
            Long endMileage =task.getToPointMileage();
            Boolean includeBegin = null;

            //检测时间最新的第一段100%需要完整加入
            if(pipeLineExinspTaskList.isEmpty()){
                PipeLineNewTaskVO noJoin = new PipeLineNewTaskVO();
                noJoin.setId(id);
                noJoin.setStartMileage2(beginMileage);
                noJoin.setEndMileage2(endMileage);
                noJoin.setIncludeStart(true);
                noJoin.setIncludeEnd(true);

                pipeLineExinspTaskList.add(noJoin);
            }

            //循环对比
            for(int j = 0; j < pipeLineExinspTaskList.size(); j++)
            {
                //待加入的元素
                PipeLineNewTaskVO noJion = new PipeLineNewTaskVO();
                noJion.setId(id);
                noJion.setStartMileage2(beginMileage);
                noJion.setEndMileage2(endMileage);

                //已加入的元素
                PipeLineNewTaskVO jioned = pipeLineExinspTaskList.get(j);
                Long beginMileage_jioned= jioned.getStartMileage2();
                Long endMileage_jioned= jioned.getEndMileage2();

                //如果待加入的元素终点绝对距离小于已加入的元素起点绝对距离
                if(endMileage != null && endMileage.compareTo(beginMileage_jioned) == -1){
                    //在前面，终点比起点小
                    noJion.setIncludeStart(includeBegin == null ? true : includeBegin);
                    noJion.setIncludeEnd(true);
                    pipeLineExinspTaskList.add(j++, noJion);//加在当前已加入元素的前面
                    break;
                }

                //如果待加入的元素起点绝对距离小于已加入元素起点绝对距离并且待加入的元素终点绝对距离大于等于已加入元素起点绝对距离
                if(beginMileage != null && endMileage != null && beginMileage.compareTo(beginMileage_jioned) == -1 && endMileage.compareTo(beginMileage_jioned) > -1){
                    //有交叉，起点比起点小，终点比起点大等于。
                    noJion.setIncludeStart(includeBegin == null ? true : includeBegin);
                    noJion.setIncludeEnd(false);
                    noJion.setEndMileage2(beginMileage_jioned);
                    pipeLineExinspTaskList.add(j++, noJion);//加在当前已加入元素的前面
                    
                    beginMileage = beginMileage_jioned;//切断
                }

                //如果待加入的元素起点绝对距离小于等于已加入元素终点绝对距离并且终点绝对距离大于已加入元素终点绝对距离
                if(beginMileage != null && endMileage != null && beginMileage.compareTo(endMileage_jioned) < 1 && endMileage.compareTo(endMileage_jioned) == 1){
                    //有交叉，起点比终点小等于，终点比终点大。
                    if(j == pipeLineExinspTaskList.size() - 1){//只有处于最后一个了，才往里面加。
                        noJion.setIncludeStart(includeBegin == null ? false : includeBegin);
                        noJion.setIncludeEnd(true);
                        noJion.setStartMileage2(endMileage_jioned);
                        pipeLineExinspTaskList.add(++j, noJion);//加在当前元素的后面
                    }
                    beginMileage = endMileage_jioned;//切断。
                    includeBegin = false;//不能包含起点了。
                }

                //如果待加入的元素起点大于已加入元素终点
                if(beginMileage != null && endMileage_jioned != null && pipeLineExinspTaskList != null && beginMileage.compareTo(endMileage_jioned) == 1  && j == pipeLineExinspTaskList.size() - 1){
                    //在后面，起点比终点大
                    if(j == pipeLineExinspTaskList.size() - 1){//只有处于最后一个了，才往里面加。
                        noJion.setIncludeStart(includeBegin == null ? true : includeBegin);
                        noJion.setIncludeEnd(true);
                        pipeLineExinspTaskList.add(++j, noJion);//加在当前已加入元素的后面
                    }
//                    break;
                }
            }
        }
        return pipeLineExinspTaskList;
    }
    
}
